<template>
	<view>
		<u-sticky :enable="stickyEnable">
			<view class="padding-sm bg-white">
				<u-search placeholder="搜索名称" v-model="page.cond.name" @search="search" @custom="search"></u-search>
			</view>
		</u-sticky>
		<tui-list-view v-for="(item,index) in result.records" :key="index">
			<tui-swipe-action :actions="actions" :params="item" @click="actionHandler">
				<template v-slot:content>
					<tui-list-cell :arrow="true" @click.native="childList(item)">
						<view class="text-lg text-bold text-cut">{{item.name}}</view>
						<view class="text-gray">{{item.code}}</view>
					</tui-list-cell>
				</template>
			</tui-swipe-action>
		</tui-list-view>
		<u-loadmore :status="result.current < result.pages?'loading':'nomore'" />
	</view>
</template>

<script>
	export default {
		onLoad: function(option) {
			if (option.parentId) {
				this.page.cond.parentId = option.parentId || null;
				this.parentId = option.parentId || null;
			}
			this.list();
		},
		onShow: function() {
			this.stickyEnable = true;
		},
		onHide: function() {
			this.stickyEnable = false;
		},
		onReady: function() {},
		onReachBottom: function() {
			if (this.result.current < this.result.pages) {
				this.page.current += 1;
				this.list();
			}
		},
		onNavigationBarButtonTap: function(option){
			if (option.type == "home") {
				uni.navigateBack({
					delta: getCurrentPages().length - 1
				});
			}
		},
		data() {
			return {
				stickyEnable: false, // 吸顶组件在页面隐藏时禁用
				actions: [{
					name: '人员',
					width: 70,
					fontsize: 30,
					color: '#fff',
					background: '#00aaff'
				}], // 滑动菜单动作配置
				page: {
					current: 1,
					size: 20,
					total: 0,
					orders: [{
						column: "seq",
						asc: true
					}],
					cond: {},
				}, // 分页查询请求
				result: {
					current: 1,
					pages: 1,
					records: []
				}, // 分页查询结果
				parentId: null, // 父组织ID
			};
		},
		methods: {
			// 查询
			search() {
				this.page.current = 1;
				this.list();
			},
			// 分页查询
			list() {
				this.$http.post("/admin/sys/rbac/org/list", this.page, res => {
					let result = res.data || this.result;
					if (this.page.current > 1) {
						result.records = this.result.records.concat(result.records);
					}
					if (!this.parentId) {
						let records = [{id: '', name: '全部人员', code: 'ALL'}];
						records = records.concat(result.records);
						records = records.concat([{id: 'single', name: '独立人员', code: 'SINGLE'}]);
						result.records = records;
					}
					this.result = result;
				}, false, false, true);
			},
			// 子组织
			childList(item) {
				if (item.id && item.id != 'single') {
					uni.navigateTo({
						url: `org-list?parentId=${item.id}`,
					});
				} else {
					// 如果点击的是全部人员，或独立用户，则直接跳转到人员列表
					uni.navigateTo({
						url: `user-list?orgId=${item.id}`,
					});
				}
			},
			// 滑动菜单事件处理
			actionHandler(obj) {
				// obj.index, obj.item:params
				if (obj.index === 0) {
					uni.navigateTo({
						url: `user-list?orgId=${obj.item.id}`,
					});
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
</style>
